pub fn scalar_type() {
    println!("{}", 1i32 - 2);
}
pub fn tuple_type() {
    fn reverse(pair: (i32, bool)) -> (bool, i32) {
        let (int, boolean) = pair;
        (boolean, int)
    }
    let pair = (1, true);
    println!("pair is {:?}", pair);
    println!("the reversed pair is {:?}", reverse(pair));

    #[derive(Debug)]
    struct Matrix(f32, f32, f32, f32);

    let long_tuple = (
        1u8, 2u16, 3u32, 4u64, -1i8, -2i16, -3i32, -4i64, 0.1f32, 0.2f64, 'a', false,
    );
    println!("too long tuple: {:?}", long_tuple);
    // 创建单元素元组需要一个额外的逗号，这是为了和被括号包含的字面量作区分。
    println!("one element tuple: {:?}", (5u32,));
    println!("just an integer: {:?}", (5u32));
    use std::fmt;
    impl fmt::Display for Matrix {
        fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
            writeln!(f, "( {} {} )", self.0, self.1);
            write!(f, "( {} {} )", self.2, self.3)
        }
    }
    let matrix = Matrix(1.1, 1.2, 2.1, 2.2);
    println!("{}", matrix);
    fn transpose(ma: Matrix) {
        let Matrix(a, b, c, d) = ma;
        println!("Matrix:\n{}", ma);
        println!("Transpose:\n{}", Matrix(a, c, b, d));
    }
    transpose(matrix);
}
pub fn slice_test() {
    use std::mem;
    fn analyze_slice(slice: &[i32]) {
        println!("{}", slice[0]);
        println!("{}", slice.len());
    }
    // 定长数组（类型标记是多余的）
    let xs: [i32; 5] = [1, 2, 3, 4, 5];
    // 所有元素可以初始化成相同的值
    let ys: [i32; 500] = [0; 500];
    // 下标从 0 开始
    println!("first element of the array: {}", xs[0]);
    println!("second element of the array: {}", xs[1]);

    // `len` 返回数组的大小
    println!("array size: {}", xs.len());

    // 数组是在栈中分配的
    println!("array occupies {} bytes", mem::size_of_val(&xs));

    // 数组可以自动被借用成为 slice
    println!("borrow the whole array as a slice");
    analyze_slice(&xs);

    // slice 可以指向数组的一部分
    println!("borrow a section of the array as a slice");
    analyze_slice(&ys[1..4]);

    // 越界的下标会引发致命错误（panic）
    //println!("{}", xs[5]);
}
